const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//id
    code:"",//从通知1进来还是我的进来2
    title:"",//标题
    content:"", //内容
    staffIdAll:0,//添加时候的接收人
    staffNameAll:"", //添加时候的接收人
    recipient:"", //修改时候的接收人
    noticeData:"",//获取到的数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      code:options.code,
      staffIdAll: options.staffIdAll,
      staffNameAll: options.staffNameAll
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNoticeMesData(); //获取详情
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /*删除操作*/
  delete:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除此公告吗？',
      success(res) {
        if (res.confirm) {
          that.getDeleteMesData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //获取详情
  getNoticeMesModel:new BaseModel({
    url: "employees/detailNotice",
    needAjaxLoading:true,
    method:"get",
    loadingText:"玩命加载中"
  }),
  getNoticeMesData(){
    this.getNoticeMesModel
      .fetch({
        id:this.data.id
      })
      .then(res => {
        this.setData({
          noticeData:res.data,
        })
      })
      .catch(res => {
        //失败 2018-12-20 00:00:00
      })
  },
  formSubmit:function(e){
    var staffIdAll = this.data.staffIdAll;
    if(e.detail.value.title==""||e.detail.value.content==""){
      wx.showToast({
        title: '标题，内容不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      title:e.detail.value.title, //标题
      content: e.detail.value.content, //内容
      recipient: e.detail.value.recipient, //修改时的接收人
      staffIdAll: this.data.staffIdAll, //添加时的接收人
    })
    if(!this.data.id){
      this.getAddNoticeMesData(); //添加
    }else{
      this.getEditNoticeMesData(); //修改
    }
  },
  //添加数据
  getAddNoticeMesModel:new BaseModel({
    url: "employees/addNotice",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getAddNoticeMesData(){
    this.getAddNoticeMesModel
        .fetch({
          title:this.data.title,
          content:this.data.content,
          recipient: this.data.staffIdAll,
        })
        .then(res => {
          wx.navigateBack({delta: 1})  //返回上级页面
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  //修改数据
  getEditNoticeMesModel:new BaseModel({
    url: "employees/upNotice",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEditNoticeMesData(){
    this.getEditNoticeMesModel
        .fetch({
          id:this.data.id,
          title:this.data.title,
          content:this.data.content,
          recipient: this.data.recipient,
        })
        .then(res => {
          wx.navigateBack({delta: 1})  //返回上级页面
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  //获取详情
  getDeleteMesModel:new BaseModel({
    url: "employees/deleteNotice",
    needAjaxLoading:true,
    method:"get",
    loadingText:"玩命加载中"
  }),
  getDeleteMesData(){
    this.getDeleteMesModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          wx.navigateBack({delta: 1})  //返回上级页面
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getNoticeMesData(); //获取详情
    wx.stopPullDownRefresh(); //停止下拉
  },
})